﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;

namespace ApplicationLOLS13
{
    internal class TeamUtil
    {
        /// 所有参赛的世界杯战队
        public static List<Team> ListTeam = new List<Team>();

        /// 初始化世界杯各个参赛战队
        public static void InitTeam()
        {
            ListTeam.Clear();
            AddTeam(new Team()
            {
                TeamName = "JDG",
                WinningRatio = 90,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\JDG.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "LNG",
                WinningRatio = 85,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\LNG.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "BLG",
                WinningRatio = 80,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\BLG.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "WBG",
                WinningRatio = 75,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\WBG.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "GEN",
                WinningRatio = 90,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\GEN.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "T1",
                WinningRatio = 85,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\T1.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "KT",
                WinningRatio = 80,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\KT.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "DK",
                WinningRatio = 75,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\DK.jpg")
            });

            AddTeam(new Team()
            {
                TeamName = "FNC",
                WinningRatio = 80,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\FNC.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "BDS",
                WinningRatio = 70,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\BDS.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "C9",
                WinningRatio = 75,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\C9.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "G2",
                WinningRatio = 70,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\G2.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "GAM",
                WinningRatio = 75,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\GAM.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "MAD",
                WinningRatio = 75,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\MAD.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "NRG",
                WinningRatio = 65,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\NRG.jpg")
            });
            AddTeam(new Team()
            {
                TeamName = "TL",
                WinningRatio = 85,
                TeamFlag = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Image\\TL.jpg")
            });
        }

        /// 添加一个战队
        public static void AddTeam(Team Team)
        {
            ListTeam.Add(Team);
        }

        /// <summary>
        /// 两个匹配的战队进行比赛，根据胜率，进行随机，返回胜利的战队
        /// 胜利法则如下：先随机出一个胜率之和之间的随机数，
        /// 如果随机数小于【最小数的2倍】并且是奇数，则认为 胜率低的战队胜利，否则 胜率高的战队胜利
        /// </summary>
        /// <param name="tuple">参赛战队</param>
        /// <param name="failTeam">失败战队</param>
        /// <returns></returns>
        public static Team SoccerGame(Tuple<Team, Team> tuple, out Team failTeam, out int randomNumber)
        {
            failTeam = tuple.Item2;
            randomNumber = -1;
            if (tuple == null || tuple.Item1 == null)
            {
                throw new Exception("世界比赛无参赛的战队或者第一个参赛战队不存在");
            }
            if (tuple.Item2 == null)
            {
                //无第二个世界参赛战队，直接躺赢【对奇数个参赛战队的特殊处理】
                return tuple.Item1;
            }
            decimal minWinningRatio = Math.Min(tuple.Item1.WinningRatio, tuple.Item2.WinningRatio);
            Team bigWin = null;//高胜率
            Team smallWin = null;//低胜率
            if (tuple.Item1.WinningRatio < tuple.Item2.WinningRatio)
            {
                bigWin = tuple.Item2;
                smallWin = tuple.Item1;
            }
            else
            {
                bigWin = tuple.Item1;
                smallWin = tuple.Item2;
            }

            int total = (int)(tuple.Item1.WinningRatio + tuple.Item2.WinningRatio);
            Random random = new Random(Guid.NewGuid().GetHashCode());
            //先随机出一个胜率之和之间的随机数
            randomNumber = random.Next(total);
            if (randomNumber < minWinningRatio * 2 && (randomNumber & 1) != 0)
            {
                //如果随机数小于【最小数的2倍】并且是奇数，则认为 胜率低的战队胜利，否则 胜率高的战队胜利
                failTeam = bigWin;
                return smallWin;
            }
            failTeam = smallWin;
            return bigWin;
        }
    }
}
